const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();
const cookieParser = require('cookie-parser');
const multer = require('multer');
const { resolve } = require('path');
//中间件
app.use(express.static(path.join(__dirname, '../static')));
app.use(express.json());
app.use(cookieParser());
app.get('/',(req,res)=>{
    // console.log(req.query);
    console.log(req.session.mailbox);
    res.render('../static/index.html');
})
app.post('/dologin', (req, res) => {
    // console.log(req.body);
    var mailbox = req.body.mailbox;
    var password = req.body.password;
    var flag = 0;
    var p = new Promise((resolve, reject) => {
         console.log(mailbox);
         console.log(password);
        const user = require('../db/user.js');
        user.find({ mailbox: mailbox }, { isVIP: 1, password: 1 }, (err, data) => {
            if (err) {
                console.log(err);
                return;
            }
            var userinfo = data;
            if (userinfo[0] == undefined) {
                // console.log('aa');
                res.send('0');
                // res.end();
            }
            else {
                if (userinfo[0].password != password) {
                    // console.log('bb');
                    res.send('0');
                    // res.end();
                }
                else {
                    if(userinfo[0].isVIP =='True'){
                        console.log(flag);
                        flag = '2'
                    }
                    else flag = '1';
                    // console.log('cc');
                    resolve(flag);
                    res.send(flag);
                    // res.end();
                }
            }
        })
    })

    p.then(() => {
        // if (data == 1) {
        //     console.log(data);
        //     console.log(mailbox);
        //     console.log(password);
        //     res.cookie("mailbox",mailbox,{maxAge:600000});
        //     res.cookie("password",password,{maxAge:600000});
        //     // console.log(req.cookies);
        // }
        res.end();
    })
})
app.post('/doregister',(req,res)=>{
    var mailbox = req.body.mailbox;
    var username = req.body.username;
    var password = req.body.password;
    var nickname = req.body.nickname;
    var p = new Promise((resolve,reject) => {
        const user = require('../db/user.js');
        user.find({mailbox : mailbox},{username : 1},(err , data) => {
            if(err){
                console.log(err);
                return;
            }
            var userinfo = data;
            if(userinfo[0] == undefined){
                var newUser = new user({
                    username : username,
                    password : password,
                    isVIP : 'False',
                    mailbox : mailbox,
                    nickname : nickname
                })
                newUser.save((err)=>{
                    if(err){
                        console.log(err);
                        return;
                    }
                    else console.log('success to input a user!');
                })
                res.send('true');
            }
            else {
                res.send('false');
            }
        })
    })
    p.then(()=>{
        res.end();
    })
    
})
app.get('/setcookie', (req,res)=>{
    console.log(req.query);
    res.header("Access-Control-Allow-Origin", "*");
    res.cookie("iflogin",req.query.iflogin,{maxAge:600000});
    res.cookie("isVIP",req.query.isVIP,{maxAge:600000});
    console.log('success');
    res.end();
    
})
app.post('/paperinfo', (req, res) => {
    res.header("Access-Control-Allow-Origin", "*");
    var info = req.body;
    var papername = info.papername;
    var schoolname = info.schoolname;
    var date = info.date;
    const doc_gen = require('./doc_gen.js');
    doc_gen(papername, schoolname, date)
    res.end();
})
app.get('/iflogin', (req, res) => {
     console.log(req.cookies);
     if(req.cookies.iflogin == 'true'){
         if(req.cookies.isVIP == 'true'){
             res.send('1');
         } 
         else if(req.cookies.isVIP == 'false'){
             res.send('2');
         }
     }
     else res.send('0');
})
let upload = multer({
    storage: multer.diskStorage({
        destination:function(req,file,cb){
            cb(null,'../data/');
        },
        filename:function(req,file,cb){
            var changeName = (new Date().getTime())+'-'+file.originalname;
            cb(null,changeName);
        }
    })
});
app.post('/selfuploads',upload.single('singleFile'),(req,res)=>{
    console.log(req.file);
})


//监听端口
app.listen(8080);
console.log("WebServer is running");